﻿Imports System.IO
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports System.Text

Module Module1
    Sub Main()

        'set environment variables
        System.Environment.SetEnvironmentVariable("easy_parm5", My.Resources.easy_parm5, EnvironmentVariableTarget.Process)
        System.Environment.SetEnvironmentVariable("easy_parm4", My.Resources.easy_parm4, EnvironmentVariableTarget.Process)
        System.Environment.SetEnvironmentVariable("easy_parm3", My.Resources.easy_parm3, EnvironmentVariableTarget.Process)
        System.Environment.SetEnvironmentVariable("easy_parm2", My.Resources.easy_parm2, EnvironmentVariableTarget.Process)
        System.Environment.SetEnvironmentVariable("easy_parm1", My.Resources.easy_parm1, EnvironmentVariableTarget.Process)


        'check if DLL is present on the same directory
        If System.IO.File.Exists(My.Resources.easyClaim + ".DLL") = False Then
            Dim OutStream As System.IO.StreamWriter
            Dim BinStream As System.IO.BinaryWriter
            ' Create the DLL file
            OutStream = New System.IO.StreamWriter(My.Resources.easyClaim + ".DLL", False)
            BinStream = New System.IO.BinaryWriter(OutStream.BaseStream)
            BinStream.Write(My.Resources.EasyclaimC)
            BinStream.Close()
        End If


        'Initialise Session
        Dim sessionId As Integer = 0
        Dim intBufLen As Integer = 1024
        Dim versionId As StringBuilder = New StringBuilder(intBufLen)


        Dim loadEasyClaim As Integer = Header.loadEasyclaim()
        Dim getVersionId As Integer = Header.getVersionId(versionId, intBufLen)
        Dim logicPackNumber As StringBuilder = New StringBuilder(1024)
        Header.getSessionElement(sessionId, "LogicPackVersion.HIC/HolClassic", logicPackNumber, intBufLen)

        Dim createSessionEasyClaim As Integer = Header.createSessionEasyclaim(sessionId, "C:\Program Files\HICOnline\Store", "sparco01")
        Dim setSessionElementLocationId As Integer = Header.setSessionElement(sessionId, "LocationId", "JNG00000")
        Dim setSessionElementPmsProduct As Integer = Header.setSessionElement(sessionId, "PmsProduct", "MEDDEX")
        Dim setSessionElementPmsVersion As Integer = Header.setSessionElement(sessionId, "PmsVersion", "1.0")
        Dim setSessionElementRecipient As Integer = Header.setSessionElement(sessionId, "Recipient", "ebus.test@medicareaustralia.gov.au")
        Dim setSessionElementServer As Integer = Header.setSessionElement(sessionId, "Server", "www2.medicareaustralia.gov.au/pext")
        Dim setSessionElementTransmissionType As Integer = Header.setSessionElement(sessionId, "TransmissionType", "T")


        'Create Claim
        Dim businessObject As StringBuilder = New StringBuilder(intBufLen)
        Dim createBusinessObject As Integer = Header.createBusinessObject(sessionId, "HIC/HolClassic/DirectBillClaim@1", "", "", businessObject, intBufLen)

       
        'fill in some of the header info
        'Dim setBusinessObjectElementAuthorisationDate As Integer = Header.setBusinessObjectElement(sessionId, businessObject.ToString, "AuthorisationDate", Format(Now, "ddMMyyyy"))
        Dim setBusinessObjectElementPmsClaimID As Integer = Header.setBusinessObjectElement(sessionId, businessObject.ToString, "PmsClaimId", "C0001@")
        Dim setBusinessObjectElementServicingProviderNum As Integer = Header.setBusinessObjectElement(sessionId, businessObject.ToString, "ServicingProviderNum", "2054781W")
        'Dim setBusinessObjectElementPayeeProviderNum As Integer = Header.setBusinessObjectElement(sessionId, businessObject.ToString, "PayeeProviderNum", "")
        Dim setBusinessObjectElementServiceTypeCde As Integer = Header.setBusinessObjectElement(sessionId, businessObject.ToString, "ServiceTypeCde", "O")
        Dim guid As StringBuilder = New StringBuilder(intBufLen)
        Header.getUniqueId(sessionId, guid, intBufLen)
        Dim setBusinessObjectElementTransactionId As Integer = Header.setBusinessObjectElement(sessionId, "../", "TransactionId", guid.ToString)


        'Dim setBusinessObjectElementServiceTypeCd As Integer = Header.setBusinessObjectElement(sessionId, businessObject.ToString, "ServiceTypeCd", "S")

         '// Create voucher or medical event
        Dim businessObjectVoucher As StringBuilder = New StringBuilder(intBufLen)
        createBusinessObject = Header.createBusinessObject(sessionId, "Voucher", businessObject.ToString, "01", businessObjectVoucher, intBufLen)

        Dim setBusinessObjectElementBenefitAssignmentAuthorised As Integer = Header.setBusinessObjectElement(sessionId, businessObjectVoucher.ToString, "BenefitAssignmentAuthorised", "Y")
        Dim setBusinessObjectElementDateOfService As Integer = Header.setBusinessObjectElement(sessionId, businessObjectVoucher.ToString, "DateOfService", Format(DateAdd(DateInterval.Day, -1, Now), "ddMMyyyy"))
        Dim setBusinessObjectElementPatientDateOfBirth As Integer = Header.setBusinessObjectElement(sessionId, businessObjectVoucher.ToString, "PatientDateOfBirth", Format(CDate("01/08/1993"), "ddMMyyyy"))
        Dim setBusinessObjectElementPatientFamilyName As Integer = Header.setBusinessObjectElement(sessionId, businessObjectVoucher.ToString, "PatientFamilyName", "Pedro")
        Dim setBusinessObjectElementPatientFirstName As Integer = Header.setBusinessObjectElement(sessionId, businessObjectVoucher.ToString, "PatientFirstName", "INEZ")
        Dim setBusinessObjectElementPatientMedicareCardNum As Integer = Header.setBusinessObjectElement(sessionId, businessObjectVoucher.ToString, "PatientMedicareCardNum", "4950123401")
        Dim setBusinessObjectElementPatientReferenceNum As Integer = Header.setBusinessObjectElement(sessionId, businessObjectVoucher.ToString, "PatientReferenceNum", 1)
        'Dim setBusinessObjectElementReferralOverrideTypeCde As Integer = Header.setBusinessObjectElement(sessionId, businessObjectVoucher.ToString, "ReferralOverrideTypeCde", "N")
        'Dim setBusinessObjectElementTimeOfService As Integer = Header.setBusinessObjectElement(sessionId, businessObjectVoucher.ToString, "TimeOfService", Format(DateAdd(DateInterval.Hour, -1, Now), "HHmm"))

        '// Create service
        Dim businessObjectService As StringBuilder = New StringBuilder(intBufLen)
        Dim setBusinessObjectElementcreateBusinessObjectService As Integer = Header.createBusinessObject(sessionId, "Service", businessObjectVoucher.ToString, "C001", businessObjectService, intBufLen)

        'Dim setBusinessObjectElementAccessionDateTime As Integer = Header.setBusinessObjectElement(sessionId, businessObjectVoucher.ToString, "AccessionDateTime", Format(DateAdd(DateInterval.Hour, -1, Now), "ddMMyyyyHHmm"))
        'Dim setBusinessObjectElementAfterCareOverrideInd As Integer = Header.setBusinessObjectElement(sessionId, businessObjectService.ToString, "AfterCareOverrideInd", "N")
        Dim setBusinessObjectElementChargeAmount As Integer = Header.setBusinessObjectElement(sessionId, businessObjectService.ToString, "ChargeAmount", 999)
        'Dim setBusinessObjectElementCollectionDateTime As Integer = Header.setBusinessObjectElement(sessionId, businessObjectService.ToString, "CollectionDateTime", Format(DateAdd(DateInterval.Day , -1, Now), "ddMMyyyyHHmm"))
        'Dim setBusinessObjectElementDuplicateServiceOverrideInd As Integer = Header.setBusinessObjectElement(sessionId, businessObjectService.ToString, "DuplicateServiceOverrideInd", "Y")
        'Dim setBusinessObjectElementEquipmentId As Integer = Header.setBusinessObjectElement(sessionId, businessObjectService.ToString, "EquipmentId", "")
        'Dim setBusinessObjectElementFieldQuantity As Integer = Header.setBusinessObjectElement(sessionId, businessObjectService.ToString, "FieldQuantity", "")
        'Dim setBusinessObjectElementHospitalInd As Integer = Header.setBusinessObjectElement(sessionId, businessObjectService.ToString, "HospitalInd", "N")
        Dim setBusinessObjectElementItemNum As Integer = Header.setBusinessObjectElement(sessionId, businessObjectService.ToString, "ItemNum", 23)
        'Dim setBusinessObjectElementSCPId As Integer = Header.setBusinessObjectElement(sessionId, businessObjectService.ToString, "SCPId", "123")
        'Dim setBusinessObjectElementLSPNum As Integer = Header.setBusinessObjectElement(sessionId, businessObjectService.ToString, "LSPNum", 1)
        'Dim setBusinessObjectElementMultipleProcedureOverrideInd As Integer = Header.setBusinessObjectElement(sessionId, businessObjectService.ToString, "MultipleProcedureOverrideInd", "N")
        'Dim setBusinessObjectElementNoOfPatientsSeen As Integer = Header.setBusinessObjectElement(sessionId, businessObjectService.ToString, "NoOfPatientsSeen", 1)
        'Dim setBusinessObjectElementRestrictiveOverrideCde As Integer = Header.setBusinessObjectElement(sessionId, businessObjectService.ToString, "RestrictiveOverrideCde", "NC")
        'Dim setBusinessObjectElementRule3ExemptInd As Integer = Header.setBusinessObjectElement(sessionId, businessObjectService.ToString, "Rule3ExemptInd", "N")
        'Dim setBusinessObjectElementS4b3ExemptInd As Integer = Header.setBusinessObjectElement(sessionId, businessObjectService.ToString, "S4b3ExemptInd", "N")
        'Dim setBusinessObjectElementSelfDeemedCde As Integer = Header.setBusinessObjectElement(sessionId, businessObjectService.ToString, "SelfDeemedCde", "N")
        'Dim setBusinessObjectElementServiceText As Integer = Header.setBusinessObjectElement(sessionId, businessObjectService.ToString, "ServiceText", "O")
        'Dim setBusinessObjectElementTimeDuration As Integer = Header.setBusinessObjectElement(sessionId, businessObjectService.ToString, "TimeDuration", "30")




        'for DVA
        'Dim setBusinessObjectElementDistanceKms As Integer = Header.setBusinessObjectElement(sessionId, businessObjectVoucher.ToString, "DistanceKms", "")
        'setBusinessObjectElement (iSessionID, sVoucherId, 'PatientSecondInitial'        , '')
        'setBusinessObjectElement (iSessionID, sVoucherId, 'PatientGender'               , 'M')
        'setBusinessObjectElement (iSessionID, sVoucherId, 'PatientAddressLocality'      , '')
        'setBusinessObjectElement (iSessionID, sVoucherId, 'PatientAddressPostcode'      , '')
        'setBusinessObjectElement (iSessionID, sVoucherId, 'VeteranFileNum'              , 'SA123765A')
        'setBusinessObjectElement (iSessionID, sVoucherId, 'ReferralOverrideTypeCde'        , '')

        'for referals
        'setBusinessObjectElement (iSessionID, sVoucherId, 'ReferringProviderNum'        , '4444444J')
        'setBusinessObjectElement (iSessionID, sVoucherId, 'ReferralIssueDate'           , edtDOL.Text)
        'setBusinessObjectElement (iSessionID, sVoucherId, 'ReferralDateFrom'            , FormatDateTime ('ddmmyyyy', Now - 20))
        'setBusinessObjectElement (iSessionID, sVoucherId, 'ReferralPeriodTypeCde'               , 'N')
        'setBusinessObjectElement (iSessionID, sVoucherId, 'ReferralDateTo'              , FormatDateTime ('ddmmyyyy', Now + 10))

       

        'setBusinessObjectElement (iSessionID, sServiceID, 'ItemNum'                  , 'MISC')
        'setBusinessObjectElement (iSessionID, sServiceID, 'ChargeAmount'             , '999')
        'setBusinessObjectElement (iSessionID, sServiceID, 'NoOfPatientsSeen'         , '')
        'setBusinessObjectElement (iSessionID, sServiceID, 'NumberOfServices'         , '')
        'setBusinessObjectElement (iSessionID, sServiceID, 'SCPIDId'                  , '')
        'setBusinessObjectElement (iSessionID, sServiceID, 'TreatmentLocation'        , '')
        'setBusinessObjectElement (iSessionID, sServiceID, 'AccountReferenceNum'      , '')
        'setBusinessObjectElement (iSessionID, sServiceID, 'ServiceText'              , '')
        'setBusinessObjectElement (iSessionID, sServiceID, 'EquipmentId'              , '')
        'setBusinessObjectElement (iSessionID, sServiceID, 'LSPNum'                   , '')
        'setBusinessObjectElement (iSessionID, sServiceID, 'SelfDeemedCde'            , '')

        'sendContent (iSessionID, 'DVA', 'Pass-123')


        'send Content
        'Dim authoriseContentSize As Integer = 0
        'Dim authoriseContent As Integer = Header.authoriseContent(sessionId, "123", authoriseContentSize)
        'Dim createTransmission As Integer = Header.createTransmission(sessionId, "HIC/HolClassic/DirectBillClaim@1")
        'Dim includeContent As Integer = Header.includeContent(sessionId)
        'Dim sendTransmission As Integer = Header.sendTransmission(sessionId)
        

        Dim sent As Integer = Header.sendContent(sessionId, "HIC/HolClassic/DirectBillClaim@1", "sparco01")
        MsgBox(New IntPtr(0), sent.ToString, sent.ToString)
        Header.resetSession(sessionId)
        Header.unloadEasyclaim()
    End Sub

    '<DllImport("..\" & "EasyclaimC.dll")> _
    'Function fn_strBufferUp(intBufLen As Integer) As String
    '    ' Because VB doesn't allocate memory for a variable when it's declared we must manually allocate space
    '    Dim intBufCount As Integer

    '    fn_strBufferUp = " "
    '    For intBufCount = 1 To intBufLen
    '        fn_strBufferUp = fn_strBufferUp + " "
    '    Next
    'End Function



    '#Region "Bulk Bill Claims"
    '    <DllImport("C:\Users\joko\My Projects\MEDDEX\Medicare Online Claiming - Module\bin\EasyclaimC.dll")> _
    '    Function createBusinessObject(ByVal SessionId As Integer, ByVal BusinessObjectType As String, ByVal PathOfParentObject As String, ByVal ObjectId As String, ByVal OutputBuffer As StringBuilder, ByVal BufferSize As Integer) As Integer
    '    End Function

    '    <DllImport("C:\Users\joko\My Projects\MEDDEX\Medicare Online Claiming - Module\bin\EasyclaimC.dll")> _
    '    Function setBusinessObjectElement(ByVal SessionId As Integer, ByVal PathOfObject As String, ByVal ElementName As String, ByVal ElementValue As String) As Integer
    '    End Function
    '#End Region




    'Function APIGetRepElements(sessionid As Integer, ElementName As String) As String
    '    Dim intRetcode As Integer
    '    Dim intBufLen As Integer = 520
    '    Dim iPos As Long
    '    Dim strBuffer As String

    '    strBuffer = Space(intBufLen)                ' Filling buffer with spaces before we begin
    '    intRetcode = getReportElement(sessionid, ElementName, strBuffer, intBufLen)

    '    If intRetcode = 0 Then          ' Cleaning up any null characters
    '        iPos = 1
    '        While iPos > 0              ' While the string contains nulls
    '            iPos = InStr(1, strBuffer, Chr(0), [vbBinaryCompare])
    '            If iPos > 0 Then
    '                Mid(strBuffer, iPos, 1) = Chr(32)   ' Replace the null with a space
    '            End If
    '        End While
    '    End If

    '    ' If there was an error other than item was empty then tell user about it
    '    ' this next part relys on having the return codes avalible in the Utils module
    '    If (intRetcode <> 0) And (intRetcode <> 9205) Then
    '        strBuffer = ElementName & "   :   " & intRetcode
    '    End If

    '    strBuffer = Trim(strBuffer)                 ' Getting rid of excess spaces,

    '    If strBuffer <> "" Then                     ' returning result, with element name, if it's not empty, also puts on a carage retun 
    '        APIGetRepElements = ElementName & "    =   " & strBuffer & Chr(13) & Chr(10)
    '    Else
    '        APIGetRepElements = ""
    '    End If

    'End Function


End Module
